const mongoose = require('mongoose')
const Schema = mongoose.Schema
const ObjectId = Schema.Types.ObjectId

const PlaceSchema = new Schema({
    placeName: {
        type: String,
        required: true
    },
    things: [{
      type: ObjectId,
      ref: "Thing"
    }],
    meta: {
        createdAt: {
            type: Date,
            default: Date.now()
        },
        updatedAt: {
            type: Date,
            default: Date.now()
        }
    }
})

PlaceSchema.pre('save', function (next) {
    if( this.isNew ) {
        this.meta.createdAt = this.meta.updatedAt = Date.now()
    } else {
        this.meta.updatedAt = Date.now()
    }

    next()
})

mongoose.model('Place', PlaceSchema)